package com.mes.web.controller.cb;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.task.Task;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.mes.entity.cb.Order;
import com.mes.entity.cb.PageBean;
import com.mes.service.cb.OrderService;
import com.mes.utils.CustomUtils;
import com.mes.utils.DateJsonValueProcessor;
import com.mes.utils.ResponseUtil;
import com.mes.utils.StringUtil;

/**
 * 订单
 * @ClassName: OrderConrtoller 
 * @Description:
 * @Author cb
 * @Date 2018年1月15日 下午4:10:44 
 * @version 1.0
 */
@Controller
@RequestMapping("/order")
public class OrderConrtoller {

	@Resource
	private TaskService taskService;

	@Resource
	private RuntimeService runtimeService;

	@Resource
	private OrderService orderService;
	
	/**
	 * 订单分页
	 * @param end_time 结束时间
	 * @param atart_time 开始时间
	 * @param response 
	 * @param limit 页面大小
	 * @param offset 当前页
	 * @param supper 供应商
	 * @throws Exception
	 */
	@RequestMapping("/order_list")
	public void order_list(String end_time , String atart_time ,HttpServletResponse response,String limit,String offset,String supper) throws Exception {
		PageBean pages = CustomUtils.paging(limit, offset);
		
		if (StringUtil.isNotEmpty(atart_time) && StringUtil.isNotEmpty(end_time)) {
			pages.setAtartTime(atart_time);
			pages.setEndTime(end_time);
		}
		
		
		if (!"0".equals(supper)) {
			pages.setSupper(supper);
		}
		
		List<Order> orders= orderService.order_list(pages);
		
		int total= orderService.order_total(pages);                                                                                                        
		
		
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerJsonValueProcessor(java.util.Date.class,new DateJsonValueProcessor("yyyy-MM-dd"));
		JSONArray jsonArray = JSONArray.fromObject(orders, jsonConfig);
		jsonObject.put("total", total);
		jsonObject.put("rows", jsonArray);
		ResponseUtil.write(response, jsonObject);
	}
	
	/**
	 * 删除
	 * @param number 订单编号
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping("/order_Delete")
	public void order_Delete(String number,HttpServletResponse response) throws Exception {
		int total=orderService.order_Delete(number);
		JSONObject jsonObject=new JSONObject();
		
		if (total>0) {
			jsonObject.put("success", true);
		} else {
			jsonObject.put("success", false);
		}
		ResponseUtil.write(response, jsonObject);
	}

	@RequestMapping("/startApply")
	public void startApply(String id,  HttpServletResponse response) throws Exception {


		//启动流程时例
		/*Map<String, Object> variables = new HashMap<>();

		variables.put("manager","张三,李四");

		variables.put("fman","王五,赵六");

		variables.put("gman","王梅");


		ProcessInstance pi = runtimeService.startProcessInstanceByKey("myOrder", variables);
		Task task = taskService.createTaskQuery().processInstanceId(pi.getProcessInstanceId()).singleResult();
		//审批完成
		taskService.complete(task.getId());*/

		//将"未审批"改为审批中

		Map<String, Object> map = new HashMap<>();
		map.put("id", id);
		map.put("state", "2");
		map.put("remark2", "");

		int total = orderService.order_updateByState(map);
		JSONObject jsonObject = new JSONObject();
		if (total > 0) {
			jsonObject.put("success", true);
		} else {
			jsonObject.put("success", false);
		}

		ResponseUtil.write(response, jsonObject);
	}

	@RequestMapping("/order_ByTaskId")
	public void order_ByTaskId(String taskId,HttpServletResponse response ) throws Exception {
		Task singleResult = taskService.createTaskQuery().taskId(taskId).singleResult();
		String processInstanceId = singleResult.getProcessInstanceId();
		Order order=orderService.order_ByTaskId(processInstanceId);
		Gson gson=new Gson();
		ResponseUtil.write(response, gson.toJson(order));
	}

	@RequestMapping("/order_updatePage")
	public String order_updatePage(String number,String remark3, Model model,String spperId) {
		model.addAttribute("number",number);
		model.addAttribute("remark3",remark3);
		model.addAttribute("spperId", spperId);
		return "cb/order_update";
	}
	
	/**
	 * 
	 * @param order_number 采购订单编号
	 * @param supper_id 供应商id
	 * @throws Exception 
	 */
	@RequestMapping("/order_updateSupperBynumber")
	public void order_updateSupperBynumber(String number,String supper_id,HttpServletResponse response) throws Exception {
		
		Map<String, Object> map = new HashMap<>();
		
		map.put("order_number", number);
		map.put("supper_id", supper_id);
		
		int effect = orderService.order_updateSupperBynumber(map);
		
		JSONObject jsonObject=new JSONObject();
		
		if (effect > 0) {
			jsonObject.put("success", true);
		} else {
			jsonObject.put("success", false);
		}
		
		ResponseUtil.write(response, jsonObject);
	}


}
